package com.hc.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.hc.action.MapAction;
import com.hc.action.MapAreaAction;
import com.hc.bean.BaseStationBean;
import com.hc.map.MapAreaDot;
import com.hc.map.MapManagement;

public class MapServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String actionType = req.getParameter("actionType");  
		if("initMap".equals(actionType)){
			int mapId = Integer.parseInt(req.getParameter("mapId"));
			returnMsg(req, resp, MapAction.initMap(mapId));
		}else if("getAreaById".equals(actionType)){
			int mapId = Integer.parseInt(req.getParameter("mapId"));
			int areaId = Integer.parseInt(req.getParameter("areaId"));
			returnMsg(req, resp, MapAction.getAreaById(mapId, areaId));
		}else if("getNetById".equals(actionType)){
			int mapId = Integer.parseInt(req.getParameter("mapId"));
			int netId = Integer.parseInt(req.getParameter("netId"));
			returnMsg(req, resp, MapAction.getNetById(mapId, netId));
		}else if("initMapKeys".equals(actionType)){
			returnMsg(req, resp, MapAction.initMapKeys());
		}else if("getBaseById".equals(actionType)){
			int mapId = Integer.parseInt(req.getParameter("mapId"));
			int baseId = Integer.parseInt(req.getParameter("baseId"));
			returnMsg(req, resp, MapAction.getBaseById(mapId, baseId));
		}else if("addBase".equals(actionType)){
			int mapId = Integer.parseInt(req.getParameter("mapId"));
			String name = req.getParameter("name");
			int x = Integer.parseInt(req.getParameter("x"));
			int y = Integer.parseInt(req.getParameter("y"));
			returnJson(req, resp, MapAction.addBase(mapId, name, x, y));
		}else if("deleteBase".equals(actionType)){
			int mapId = Integer.parseInt(req.getParameter("mapId"));
			int baseId = Integer.parseInt(req.getParameter("baseId"));
			MapAction.deleteBase(mapId, baseId);
		}else if("addArea".equals(actionType)){
			int mapId = Integer.parseInt(req.getParameter("mapId"));
			returnJson(req, resp, MapAction.addArea(mapId));
		}else if("deleteArea".equals(actionType)){
			int mapId = Integer.parseInt(req.getParameter("mapId"));
			int areaId = Integer.parseInt(req.getParameter("areaId"));
			MapAction.deleteArea(mapId, areaId);
		}else if("addAreaDot".equals(actionType)){
			int mapId = Integer.parseInt(req.getParameter("mapId"));
			int areaId = Integer.parseInt(req.getParameter("areaId"));
			int x = Integer.parseInt(req.getParameter("x"));
			int y = Integer.parseInt(req.getParameter("y"));
			MapAction.addAreaDot(mapId, areaId, x, y);
		}else if("addNet".equals(actionType)){
			int mapId = Integer.parseInt(req.getParameter("mapId"));
			returnJson(req, resp, MapAction.addNet(mapId));
		}else if("deleteNet".equals(actionType)){
			int mapId = Integer.parseInt(req.getParameter("mapId"));
			int netId = Integer.parseInt(req.getParameter("netId"));
			MapAction.deleteNet(mapId, netId);
		}else if("initAllBase".equals(actionType)){
			int mapId = Integer.parseInt(req.getParameter("mapId"));
			returnMsg(req, resp, MapAction.initAllBase(mapId));
		}else if("editNetBase".equals(actionType)){
			int mapId = Integer.parseInt(req.getParameter("mapId"));
			int netId = Integer.parseInt(req.getParameter("netId"));
			String selectIds = req.getParameter("basesIds");
			MapAction.editNetBase(mapId, netId, selectIds);
		}else if("updateBase".equals(actionType)){
			int mapId = Integer.parseInt(req.getParameter("mapId"));
			int netId = Integer.parseInt(req.getParameter("netId"));
			int baseId = Integer.parseInt(req.getParameter("baseId"));
			int shortAddress = Integer.parseInt(req.getParameter("shortAddress"));;
			int gongLv = Integer.parseInt(req.getParameter("gongLv"));
			int channel = Integer.parseInt(req.getParameter("channel"));
			int f_start = Integer.parseInt(req.getParameter("f_start"));
			int f_end = Integer.parseInt(req.getParameter("f_end"));
			int f_step = Integer.parseInt(req.getParameter("f_step"));
			int distance_Offset = Integer.parseInt(req.getParameter("distance_Offset"));
			returnJson(req, resp, MapAction.updateBase(mapId, netId, baseId, shortAddress, gongLv, channel, f_start, f_end, f_step, distance_Offset));
		}else if("getBaseByNetIdAndBaseId".equals(actionType)){
			int mapId = Integer.parseInt(req.getParameter("mapId"));
			int netId = Integer.parseInt(req.getParameter("netId"));
			int baseId = Integer.parseInt(req.getParameter("baseId"));
			returnMsg(req, resp, MapAction.getBaseByNetIdAndBaseId(mapId, netId, baseId));
		}else if("useKaerMan".equals(actionType)){
			int mapId = Integer.parseInt(req.getParameter("mapId"));
			String used = req.getParameter("selectkaerMan");
			returnJson(req, resp, MapAction.setKalMan(mapId, used));
		}else if("addMap".equals(actionType)){
			MapAction.addMap();
		}else if("deleteMap".equals(actionType)){
			int mapId = Integer.parseInt(req.getParameter("mapId"));
			MapAction.deleteMap(mapId);
		}else if("updateBaseStation".equals(actionType)){
			int mapId = Integer.parseInt(req.getParameter("mapId"));
			int baseId = Integer.parseInt(req.getParameter("baseId"));
			String baseInfo = req.getParameter("baseInfo");
			returnJson(req, resp, MapAction.updateBaseStation(mapId, baseId, baseInfo));
		}else if("updateAreaDot".equals(actionType)){
			int mapId = Integer.parseInt(req.getParameter("mapId"));
			int areaId = Integer.parseInt(req.getParameter("areaId"));
			int vedioId = Integer.parseInt(req.getParameter("vedioId"));
			String dotInfo = req.getParameter("dotInfo");
			returnJson(req, resp, MapAction.updateAreaDotById(mapId, areaId, vedioId, dotInfo));
		}else if("initMapIds".equals(actionType)){
			returnMsg(req, resp, MapAction.initMapKeys());
		}
	}
	
	private void returnJson(HttpServletRequest req, HttpServletResponse resp, boolean isTrue){
		JSONObject json = new JSONObject();
		json.put("success", isTrue ? "true" : "false");
		returnMsg(req,resp, json.toString());
	}
	
	private void returnMsg(HttpServletRequest req, HttpServletResponse resp,
			String msg) {
		resp.setCharacterEncoding("utf-8");
		PrintWriter w;
		try {
			w = resp.getWriter();
			w.write(msg);
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	
	}
	
}
